package chen.web.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import com.gif4j.GifDecoder;
import com.gif4j.GifEncoder;
import com.gif4j.GifImage;
import com.gif4j.GifTransformer;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageResize {
	
	public final static int GIF = 0x4749;
	public final static int JPG = 0xFFD8;
	public final static int PNG = 0x8950;
	
	public static int resize(File src, File dest, int width, int height, boolean scale) throws UnsupportImageTypeException, IOException{
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		String tempFilePath = dest.getParentFile().getAbsolutePath() + File.separator + UUID.randomUUID().toString();
		File tempFile = new File(tempFilePath);
		int result = 0;
		try {
			//输入流
			FileInputStream is = new FileInputStream(src);
			bis = new BufferedInputStream(is);
			//流出流
			OutputStream os = new FileOutputStream(tempFile);
			bos = new BufferedOutputStream(os);
			byte[] tag = new byte[2];
			bis.mark(2);
			bis.read(tag);
			bis.reset();
			result =  ((int)tag[0] & 0xff) << 8 | ((int)tag[1] & 0xff);
			if(result == GIF){
				resizeGifImage(bis, bos, width, height, scale);
			}else if(result == JPG){
				resizeJPGImage(bis, bos, width, height, scale);
			}else if(result == PNG){
				resizeJPGImage(bis, bos, width, height, scale);
			}else {
				throw new UnsupportImageTypeException();
			}	
			if(dest.exists()){
				dest.delete();
			}
			tempFile.renameTo(dest);
		} finally{
			bis.close();
			bos.close();
		}
		
		return result;
	}
	
	private static void resizeGifImage(InputStream srcImg, OutputStream destImg, int width,
			int height, boolean scale) throws IOException {
		GifImage gifImage = GifDecoder.decode(srcImg);
		int h = gifImage.getScreenHeight();
		int w = gifImage.getScreenWidth();
		//源图像尺寸比设置的小
		if(h <= height && w <= width){
			height = h;
			width = w;
		}else if(scale){	//按比例缩小
			float f = w/(float)h;
			if(w > width){
				height = (int) (width / f);
			}
			if(h > height){
				width = (int) (height * f);
			}
		}
		GifImage resizedGifImage2 = GifTransformer.resize(gifImage, width,
				height, true);
		GifEncoder.encode(resizedGifImage2, destImg);
		srcImg.close();
		destImg.close();
	}
	
	private static void resizeJPGImage(InputStream srcImg, OutputStream destImg, int width,
			int height, boolean scale) throws IOException{
		BufferedImage src = javax.imageio.ImageIO.read(srcImg);
		int h = src.getHeight();
		int w = src.getWidth();
		//源图像尺寸比设置的小
		if(h <= height && w <= width){
			height = h;
			width = w;
		}else if(scale){		//按比例缩小
			float f = w/(float)h;
			if(w > width){
				height = (int) (width / f);
			}
			if(h > height){
				width = (int) (height * f);
			}
		}
        BufferedImage tag= new BufferedImage(width, height,  
                BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(src.getScaledInstance(width, height,  Image.SCALE_SMOOTH), 0, 0,  null);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(destImg);  
        encoder.encode(tag);
        destImg.close();
        srcImg.close();
	}
	
}
